package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.PageQuery;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.SysRoleDTO;
import com.ruoyi.system.domain.vo.SysRoleVO;
import com.ruoyi.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

/**
 * 角色信息
 *
 * @author ruoyi
 */
@Api(tags = "(客户端) 角色管理")
@RestController
@RequestMapping("/client/role")
public class SysRoleController {
    @Autowired
    private ISysRoleService roleService;

    @GetMapping("/list")
    @ApiOperation(value = "角色列表查询",response = SysRoleVO.class)
    public ResultEntity list(PageQuery params, @RequestParam(value = "projectId") Long projectId) {
        PageUtil.startPage(params);
        return ResultEntity.createSuccess(PageUtil.getPageList(roleService.selectRoleList(projectId)));
    }

    @GetMapping("/listAll")
    @ApiOperation(value = "查询所有角色")
    public ResultEntity listAll(@RequestParam(value = "projectId",required = true) Long projectId) {
        return ResultEntity.createSuccess(roleService.selectRoleList(projectId));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增角色")
    public ResultEntity add(@RequestBody SysRoleDTO params) {
        SysRole entity = new SysRole();
        BeanUtils.copyProperties(params,entity);
        entity.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        entity.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        return ResultEntity.createSuccess(roleService.save(entity));
    }

    @PostMapping("/update")
    @ApiOperation(value = "编辑角色")
    public ResultEntity add(@RequestBody SysRole params) {
        SysRole entity = new SysRole();
        BeanUtils.copyProperties(params,entity);
        entity.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        roleService.updateById(entity);
        return ResultEntity.createSuccess();
    }

    @PostMapping("/del/{id}")
    @ApiOperation(value = "删除角色")
    public ResultEntity del(@PathVariable("id") Long id) {
        roleService.removeById(id);
        return ResultEntity.createSuccess();
    }

    @PostMapping("/updateRoleMenu")
    @ApiOperation(value = "新增编辑角色菜单")
    public ResultEntity updateRoleMenu(@RequestBody SysRoleDTO.SysRoleMenuDTO params){
        roleService.updateRoleMenu(params);
        return ResultEntity.createSuccess();
    }

    @GetMapping("/getRoleMenu")
    @ApiOperation(value = "查询角色菜单")
    public ResultEntity getRoleMenu(@RequestParam(value = "roleId") Long roleId) {
        return ResultEntity.createSuccess(roleService.getRoleMenu(roleId));
    }

}
